Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

500
Visualizações
javascript equivalent to [x for x in array]

Is there any operation in Javascript just like [x for x in array] in python?

For example, I'm using javascript to reading a json file where there're dozens of (key, value) pairs needed to be handled(or transformed into other format). And I thought working in this way is stupid:

let transformed = []
for (let key in json){
   transformed = [ /* doing some transform*/ ]
}

Is there anything like:

let transformed = [
 lambda function1(key), lambda function2(value) for key, value in json
]

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The rough equivalent of Python's list comprehension is Array.map:

const myArray = [1, 2, 3]
const transformed = myArray.map((item) => item + 1)
// [2, 3, 4]

But your example is not about an array, but about an Object with keys and values. In Python, this would be a dict, and you'd use a dict comprehension along the lines of {function1(key): function2(value) for key, value in my_dict.items()}.

In JavaScript, you can turn such an object into an array with Object.entries, then perform the map, and finally transform it back into an object using Object.fromEntries:

const myObject = { a: 1, b: 2 }
const transformed = Object.fromEntries(Object.entries(myObject)
    .map(([key, value]) => [key + 'x', value + 1]))
// { ax: 2, bx: 3 }

Note that fromEntries is fairly new and you might need to add a polyfill for it.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a code likes this. You must use a function that handle operation on current single item.

const words = ['hello', 'bird', 'table', 'football', 'pipe', 'code'];
const capWords = words.forEach(capitalize);

function capitalize(word, index, arr) {
  arr[index] = word[0].toUpperCase() + word.substring(1);
}
console.log(words);
// Expected output:
// ["Hello", "Bird", "Table", "Football", "Pipe", "Code"]
about 4 years ago · Juan Pablo Isaza Relatório

0

First of all, javascript does NOT support Associative Arrays. If you are used to them in Python, PHP, and other languages you need to do a little workaround in JS to achieve the same functionality.

The most common way to simulate an associative array is using an object.

let testObject = {name: "Color", value: "Red"};

And then you push every object into an array so you end up with something like this:

let testArray = [{name: "Color", value: "Red"}, {name: "Color", value: "Blue"}];

Once you have this array consisting of objects, you can use map function to go through every object in the array and do whatever you want with it.

testArray.map((item, index) => {
 console.log("The value of "+index+". item is: "item.value);
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda